package com.sunnada.edu.system.login.service;

import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.sunnada.edu.system.pub.common.AppContext;
import com.sunnada.edu.system.pub.common.Constants;
import com.sunnada.edu.system.pub.dao.PubDao;
import com.sunnada.edu.system.pub.util.RSA;
import com.sunnada.edu.system.pub.util.Utils;

public class LoginMethods {

	public static void setSession(HttpServletRequest request, Map map) {
		request.getSession().setAttribute(Constants.USER_ID_SESSION,
				map.get("USER_ID"));
		request.getSession().setAttribute(Constants.USER_NAME_SESSION,
				map.get("USER_NAME"));
		request.getSession().setAttribute("USER_LOGINCODE",
				map.get("USER_LOGINCODE"));
		/*request.getSession().setAttribute(Constants.USER_TYPE,
				map.get("USER_TYPE"));
		request.getSession().setAttribute(Constants.USER_AREA_CODE,
				map.get("AREA_CODE"));
		request.getSession().setAttribute(Constants.USER_AREA_SNAME,
				map.get("AREA_NAME"));*/


		// 设置默认屏幕高度，用于分页自动算取每页记录数
		request.getSession().setAttribute(Constants.USER_SCREEN_HEIGHT, "280");
		//String userType = (String) map.get("USER_TYPE");// 0：超级管理员 1：代理 2：普通用户

	}

	/** 销毁session 2015-05-04 chs add */
	public static void removeSession(HttpServletRequest request) {
		request.getSession().removeAttribute(Constants.USER_ID_SESSION);
		request.getSession().removeAttribute(Constants.USER_NAME_SESSION);
		request.getSession().removeAttribute("USER_LOGINCODE");
		request.getSession().setAttribute(Constants.USER_ID_SESSION, null);
		request.getSession().setAttribute(Constants.USER_NAME_SESSION, null);
		request.getSession().setAttribute("USER_LOGINCODE", null);
		
		Enumeration em = request.getSession().getAttributeNames();
		while (em.hasMoreElements()) {
			request.getSession().removeAttribute(em.nextElement().toString());
		}
	}

	public static void reCheckcode(HttpServletRequest request) {
		int code = (int) ((Math.random() * 9 + 1) * 10000000);
		request.getSession().setAttribute("CheckCode", code);
	}

	public static String RSADecode(String password) {
		// password:加密长字符串(256)
		// 由于解密经常异常：若解密异常，将未加密的密码作为解密后的密码
		String pwd = password;
		try {
			password = org.apache.commons.lang.StringUtils.substring(pwd, 0,
					256);
			byte[] en_result = RSAUtil.hexStringToBytes(password);
			byte[] de_result = RSAUtil.decrypt(RSAUtil.getKeyPair()
					.getPrivate(), en_result);
			StringBuffer sb = new StringBuffer();
			sb.append(new String(de_result));
			password = sb.reverse().toString();
			password = URLDecoder.decode(password, "UTF-8");

			return password;
		} catch (Exception e) {
			e.printStackTrace();
			// logger.error("异常:RSA解密,密文:{}", pwd);
			return "";
		}
	}

	public static String RDecode(String pw) {
		try {
			return RSA.decrypt(pw, RSAUtil.getKeyPair().getPrivate());
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 修改密码
	 * 
	 * @param params
	 * @return
	 */
	public static int updatePwd(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String userId = params.get("userId");
		String password = params.get("password");
		String sql = "update S_SYSUSER set  LOGIN_PWD='" + password
				+ "' where USER_LOGINCODE='" + userId + "'";
		int num = dao.update(sql);
		return num;

	}



	/**
	 * 
	 * 根据用户编号，功能按钮权限
	 * 
	 * @param params
	 * @return
	 */
	public static String getBtnStr(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String loginUserId = params.get("loginUserId");
		String sql = " SELECT distinct T1.LINK_PATH,T1.IS_MENU "
				+ " FROM S_SYSMODULE T1, S_SYSROLEUSER T2, S_SYSMODULEROLE T3 "
				+ " WHERE T1.REC_ID = T3.MODULE_ID "
				+ " AND T3.ROLE_ID = T2.ROLE_ID " + " AND T2.USER_ID ='"
				+ loginUserId + "' AND T1.REC_STATUS = '0' "
				+ " AND T1.is_menu = '3'";
		List<?> list = dao.selectList(sql);
		String btnStr = "";
		for (int i = 0; i < list.size(); i++) {
			HashMap<String, String> d = (HashMap<String, String>) list.get(i);
			btnStr += d.get("LINK_PATH") + ";";
		}
		return btnStr;
	}

	public static List<?> getSysUser(String userLoginCode) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String sql = " select * from S_SYSUSER where USER_LOGINCODE='"
				+ userLoginCode + "'  and REC_STATUS='0'";
		return dao.selectList(sql);
	}


	

}
